package com.gdk.auth.application.controller;

import com.alibaba.fastjson.JSON;
import com.gdk.auth.application.convert.AuthPermissionDTOConverter;
import com.gdk.auth.application.convert.AuthRoleDTOConverter;
import com.gdk.auth.application.dto.AuthPermissionDTO;
import com.gdk.auth.application.dto.AuthRoleDTO;
import com.gdk.auth.common.entity.Result;
import com.gdk.auth.domain.entity.AuthPermissionBO;
import com.gdk.auth.domain.entity.AuthRoleBO;
import com.gdk.auth.domain.service.AuthPermissionDomainService;
import com.gdk.auth.domain.service.AuthRoleDomainService;
import com.google.common.base.Preconditions;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户controller
 */
@RestController
@RequestMapping("/permission/")
@Slf4j
public class PermissionController {

    @Resource
    private AuthPermissionDomainService authPermissionDomainService;

    /**
     * 新增权限
     */
    @PostMapping("add")
    public Result<Boolean> add(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("PermissionController.add.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getName()),"权限名称不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getParentId(),"权限父id不能为空");
            Preconditions.checkNotNull(authPermissionDTO.getType(),"权限类型不能为空");
            Preconditions.checkArgument(!StringUtils.isBlank(authPermissionDTO.getPermissionKey()),"权限唯一标识不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE
                    .convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.add(authPermissionBO));
        }catch (Exception e) {
            log.error("PermissionController.add.dto:{}", e.getMessage(),e);
            return Result.fail("新增权限失败");
        }
    }

    /**
     * 更新权限
     */
    @PostMapping("update")
    public Result<Boolean> update(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("PermissionController.update.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"角色id不能为空");

            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE
                    .convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.update(authPermissionBO));
        }catch (Exception e) {
            log.error("PermissionController.update.dto:{}", e.getMessage(),e);
            return Result.fail("更新权限失败");
        }
    }

    /**
     * 删除权限
     */
    @PostMapping("delete")
    public Result<Boolean> delete(@RequestBody AuthPermissionDTO authPermissionDTO) {
        try {
            if (log.isInfoEnabled()){ //在面对高并发可以更好性能，直接log.info会先序列化，而先判断则不会；
                log.info("PermissionController.delete.dto:{}", JSON.toJSONString(authPermissionDTO));
            }
            Preconditions.checkNotNull(authPermissionDTO.getId(),"角色id不能为空");
            AuthPermissionBO authPermissionBO = AuthPermissionDTOConverter.INSTANCE
                    .convertDTOToBO(authPermissionDTO);
            return Result.ok(authPermissionDomainService.delete(authPermissionBO));
        }catch (Exception e) {
            log.error("PermissionController.delete.dto:{}", e.getMessage(),e);
            return Result.fail("删除权限失败");
        }
    }

}
